import { getDepartmentMsgAPI, getDepartmentMsgListAPI, getReadListAndStatusAPI } from '@/api/depMsg'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useUserStore } from '@/stores/user'
const userInfo = useUserStore().UserInfo
export const useMessageStore = defineStore(
  'message',
  () => {
    const messageList = ref<Message[]>([])
    const readList = ref<ReadListAndStatus[]>([])

    const getReadList = async () => {
      readList.value = []
      messageList.value = []
      if (!userInfo.department) {
        return
      }
      const res = await getReadListAndStatusAPI(userInfo.id!)
      const res1 = await getDepartmentMsgListAPI(userInfo.department!)
      messageList.value = res1.data!
      if (res.data![0].read_status === 0) {
        const res = await getDepartmentMsgAPI(userInfo.id!, userInfo.department!)
        readList.value = res.data as any
      } else {
        readList.value = JSON.parse(res.data![0].read_list)
      }
    }

    return { readList, getReadList, messageList }
  },
  {
    persist: {
      // 持久化
      key: 'message'
    }
  }
)
